package com.ruoyi.applet.service.impl;

import com.ruoyi.applet.OutDTO.MedalDTO;
import com.ruoyi.applet.domain.AppletMedal;
import com.ruoyi.applet.domain.WechatUser;
import com.ruoyi.applet.mapper.AppletMedalMapper;
import com.ruoyi.applet.mapper.WechatUserMapper;
import com.ruoyi.applet.service.IAppletMedalService;
import com.brisk.common.core.domain.AjaxResult;
import com.brisk.common.utils.DateUtils;
import com.brisk.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * appletMedalService业务层处理
 *
 * @author PENGFEIDENG
 * @date 2022-05-30
 */
@Service
public class AppletMedalServiceImpl implements IAppletMedalService {
    @Autowired
    private AppletMedalMapper appletMedalMapper;

    @Autowired
    private WechatUserMapper wechatUserMapper;

    /**
     * 查询appletMedal
     *
     * @param medalId appletMedal主键
     * @return appletMedal
     */
    @Override
    public AppletMedal selectAppletMedalByMedalId(Long medalId) {
        return appletMedalMapper.selectAppletMedalByMedalId(medalId);
    }

    /**
     * 查询appletMedal列表
     *
     * @param appletMedal appletMedal
     * @return appletMedal
     */
    @Override
    public List<AppletMedal> selectAppletMedalList(AppletMedal appletMedal) {
        return appletMedalMapper.selectAppletMedalList(appletMedal);
    }

    /**
     * 新增appletMedal
     *
     * @param appletMedal appletMedal
     * @return 结果
     */
    @Override
    public int insertAppletMedal(AppletMedal appletMedal) {
        appletMedal.setCreateTime(DateUtils.getNowDate());
        appletMedal.setCreateBy(SecurityUtils.getUsername());
        return appletMedalMapper.insertAppletMedal(appletMedal);
    }

    /**
     * 修改appletMedal
     *
     * @param appletMedal appletMedal
     * @return 结果
     */
    @Override
    public int updateAppletMedal(AppletMedal appletMedal) {
        appletMedal.setUpdateTime(DateUtils.getNowDate());
        appletMedal.setUpdateBy(SecurityUtils.getUsername());
        return appletMedalMapper.updateAppletMedal(appletMedal);
    }

    /**
     * 批量删除appletMedal
     *
     * @param medalIds 需要删除的appletMedal主键
     * @return 结果
     */
    @Override
    public int deleteAppletMedalByMedalIds(Long[] medalIds) {
        return appletMedalMapper.deleteAppletMedalByMedalIds(medalIds);
    }

    /**
     * 删除appletMedal信息
     *
     * @param medalId appletMedal主键
     * @return 结果
     */
    @Override
    public int deleteAppletMedalByMedalId(Long medalId) {
        return appletMedalMapper.deleteAppletMedalByMedalId(medalId);
    }

    @Override
    public AjaxResult getUserMedalWall(Long userId) {
        //查询用户信息 获取用户现在的里程
        WechatUser wechatUser = wechatUserMapper.selectWechatUserByUserId(userId);
        if (ObjectUtils.isEmpty(wechatUser)) return AjaxResult.error("获取用户信息失败！");
        //获取徽章
        List<MedalDTO> medalDTOList = new ArrayList<MedalDTO>();
        List<AppletMedal> appletMedalList = appletMedalMapper.selectAppletMedalList(new AppletMedal());
        //过滤出已经上架的勋章
        appletMedalList = appletMedalList.stream().filter(appletMedal -> "Y".equalsIgnoreCase(appletMedal.getState())).collect(Collectors.toList());
        //循环 根据用户里程数  获取勋章
        int medalSum = 0;
        if (appletMedalList.size() > 0) {
            for (AppletMedal appletMedal : appletMedalList) {
                MedalDTO medalDTO = new MedalDTO();
                medalDTO.setUserId(wechatUser.getUserId());
                medalDTO.setMileage(appletMedal.getMedalMileage());
                medalDTO.setMedalName(appletMedal.getMedalName());
                //判断用户是否解锁
                if (wechatUser.getMileage().compareTo(appletMedal.getMedalMileage()) < 1) { //用户里程小于等于勋章解锁里程 解锁 添加解锁照片
                    medalDTO.setMedalImg(appletMedal.getMedalUnlockImg());
                } else {
                    medalDTO.setMedalImg(appletMedal.getMedalLockImg());
                    medalSum++;
                }
                medalDTOList.add(medalDTO);
            }
        }
        // 按里程数 重新排序 就可以得到勋章墙了
        medalDTOList = medalDTOList.stream().sorted(Comparator.comparing(MedalDTO::getMileage)).collect(Collectors.toList());
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("data", medalDTOList);
        ajaxResult.put("medalSum", medalSum);
        ajaxResult.put("nickName", wechatUser.getUserNickname());
        ajaxResult.put("avatarUrl", wechatUser.getUserImg());
        ajaxResult.put("userWalkSum", wechatUser.getMileage());
        return ajaxResult;
    }
}
